#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'

ll a, m, p;
string b;

ll init_p(string& b, int mod) {
  ll res = 0;
  bool flag = false;
  for (char c : b) {
    res = res * 10 + c - '0';
    if (res >= mod) flag = true, res %= mod;
  }
  if (flag) res += mod;
  return res;
}
ll phi(ll m) {
  ll res = m;
  for (ll i = 2; i * i <= m; i++) {
    if (m % i) continue;
    res = res / i * (i - 1);
    while (m % i == 0) m /= i;
  }
  if (m > 1) res = res / m * (m - 1);
  return res;
}
ll fastpow(ll b, ll p, ll mod) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> a >> m >> b;
  p = init_p(b, phi(m));
  cout << fastpow(a, p, m);
  return 0;
}